﻿using RN;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CAD.DAO
{
    public class ListDAOSQLSERVER:ListDAO
    {
        public override List<Message> ListActiveMails(string user)
        {
            CAD.DS.DSMailTableAdapters.rpt_ListMailsTableAdapter tableadapter = new CAD.DS.DSMailTableAdapters.rpt_ListMailsTableAdapter();
            CAD.DS.DSMail.rpt_ListMailsDataTable datatable = tableadapter.MailsByEstado(user, Definitions.ESTADO_ACTIVO+"");
            List<Message> List = new List<Message>();
            if (datatable != null)
            {
                foreach (CAD.DS.DSMail.rpt_ListMailsRow row in datatable)
                {
                    Message m = RowToDTO(row);
                    List.Add(m);
                }
            }
            return List;
        }
        public override List<Message> ListInActiveMails(string user)
        {
            CAD.DS.DSMailTableAdapters.rpt_ListMailsTableAdapter tableadapter = new CAD.DS.DSMailTableAdapters.rpt_ListMailsTableAdapter();
            CAD.DS.DSMail.rpt_ListMailsDataTable datatable = tableadapter.MailsByEstado(user, Definitions.ESTADO_INACTIVO + "");
            List<Message> List = new List<Message>();
            if (datatable != null)
            {
                foreach (CAD.DS.DSMail.rpt_ListMailsRow row in datatable)
                {
                    Message m = RowToDTO(row);
                    List.Add(m);
                }
            }
            return List;
        }
        public override List<Message> ListALLMails(string user)
        {
            CAD.DS.DSMailTableAdapters.rpt_ListMailsTableAdapter tableadapter = new CAD.DS.DSMailTableAdapters.rpt_ListMailsTableAdapter();
            CAD.DS.DSMail.rpt_ListMailsDataTable datatable = tableadapter.Mails(user);
            List<Message> List = new List<Message>();
            if (datatable != null)
            {
                foreach (CAD.DS.DSMail.rpt_ListMailsRow row in datatable)
                {
                    Message m = RowToDTO(row);
                    List.Add(m);
                }
            }
            return List;
        }

        private Message RowToDTO(CAD.DS.DSMail.rpt_ListMailsRow row)
        {
            Message m = new Message();
            m.Id = row.id;
            m.datoscabeceramensaje(row.From, row.To);
            m.datoscuerpomensaje(row.Subject, row.Content);
            m.Estado = row.Estado;
            m.IsRead = false;
            return m;
        }
    }
}
